package 极客算法训练营.chapter07;

import 极客算法训练营.chapter06.二叉树的中序遍历;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class 验证二叉搜索树 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution0 {
        private int count;

        public boolean isValidBST(TreeNode root) {
            _evaluate(root);
            return count == 0;
        }

        private void _evaluate(TreeNode root) {
            if (root == null) return;
            TreeNode _left = root.left;
            TreeNode _right = root.right;
            if (_left != null && _left.val >= root.val) count++;
            if (_right != null && _right.val <= root.val) count++;
            _evaluate(_left);
            _evaluate(_right);
        }
    }

    class Solution1 {

        public boolean isValidBST(TreeNode root) {
            if (root == null) return false;
            return _evaluate(root);
        }

        private boolean _evaluate(TreeNode root) {
            TreeNode _left = root.left;
            TreeNode _right = root.right;
            if (_left != null) {
                if (_left.val >= root.val || _evaluate(_left) == false)
                    return false;
            }
            if (_right != null) {
                if (_right.val <= root.val || _evaluate(_right) == false) {
                    return false;
                }
            }
            //子节点为空，回溯到底了都符合
            return true;
        }
    }

    class Solution {
        public boolean isValidBST(TreeNode root) {
            return charge(root, null, null);
        }

        public boolean charge(TreeNode root, Integer lower, Integer upper) {
            if (root == null) return true;
            int rootValue = root.val;
            if (lower != null && rootValue <= lower) return false;
            if (upper != null && rootValue >= upper) return false;
            TreeNode leftNode = root.left;
            TreeNode rightNode = root.right;
            return charge(leftNode, lower, rootValue) && charge(rightNode, rootValue, upper);
        }
    }

    class Solution3 {
        public boolean isValidBST(TreeNode root) {
            long former = Long.MIN_VALUE;
            Deque<TreeNode> stack = new LinkedList<>();
            while (root != null || !stack.isEmpty()) {
                //一直向左遍历
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                //找到上一个结点
                TreeNode pop = stack.pop();
                root = pop;
                if (root.val <= former) return false;
                former = root.val;
                //向右遍历
                root = root.right;
            }
            return true;
        }
    }

    public boolean charge(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> stack = new LinkedList<>();
        while (root != null || !stack.isEmpty()) {
            //一直向左遍历
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            //找到上一个结点
            TreeNode pop = stack.pop();
            root = pop;
            if (res.size() > 0) {
                Integer former = res.get(res.size() - 1);
                if (former != null && root.val <= former) {
                    return false;
                }
            }
            res.add(root.val);
            //向右遍历
            root = root.right;
        }
        return true;
    }
}
